import { request } from '@/api/core/http'
import { IParmasMsgHistory } from './types'
/**
 * 创建回话
 * @param gender 性别
 * @returns
 */

export const createChat = (gender: number) => {
  return request.post('/platform/api/story/h5/chat', { gender, story_id: getStoryId() })
}
// 创建会话产生的toekn
export const getToken = () => {
  return localStorage.getItem('token') || ''
}
// 创建会话产生的ai_user_id
export const getAiUserId = () => {
  return localStorage.getItem('ai_user_id') || ''
}

export const getStoryId = () => {
  return localStorage.getItem('story_id') || ''
}
/**
 * 重新挑战
 * @param token
 */
export const resetChat = () => {
  return request.post(
    '/platform/api/story/h5/reset',
    { story_id: getStoryId() },
    {
      headers: {
        Authorization: `Bearer ${getToken()}`,
      },
    },
  )
}

/**
 * 奖励列表
 * @param token
 */
export const getPrizes = () => {
  return request
    .get(`/platform/api/story/h5/intimacy_prizes`, {
      headers: {
        Authorization: `Bearer ${getToken()}`,
      },
    })
    .then(res => res.content)
}

/**
 * 奖励
 * @param percent
 */
export const getPrize = (percent: string) => {
  return request.get(`/platform/api/story/h5/intimacy_prizes/${percent}`).then(res => res.data)
}

/**
 * 完成任务之后领取奖励
 * @param percent
 */
export const getClaim = (loginToken, prize_id) => {
  const token = localStorage.getItem('token')
  return request.post(
    `/platform/api/story/h5/claim`,
    { prize_id: prize_id, story_id: getStoryId() },
    {
      headers: {
        Authorization: `Bearer ${token}`,
        token: loginToken,
      },
    },
  )
}

/**
 * 发送消息
 * @param content
 */
export const sendMsg = (content: string) => {
  return request
    .post(
      '/platform/api/story/h5/sendMsg',
      { content, ai_user_id: +getAiUserId(), story_id: getStoryId() },
      {
        headers: {
          Authorization: `Bearer ${getToken()}`,
        },
      },
    )
    .then(res => res.content)
}

/**
 * 获取消息历史
 * @param content
 */
export const getMsgHistory = (params: IParmasMsgHistory) => {
  return request
    .post(`/platform/api/story/h5/listMsgHistory`, params, {
      headers: {
        Authorization: `Bearer ${getToken()}`,
      },
    })
    .then(res => res)
}

/**获取结局列表 */
export const getEndings = () => {
  return request
    .get(`/platform/api/story/h5/ending`, {
      params: {
        story_id: getStoryId(),
      },
      headers: {
        Authorization: `Bearer ${getToken()}`,
      },
    })
    .then(res => res.content)
}
